﻿using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;

namespace Zocono.WCS.Domain.PlcPointDomain
{
    public class PlcPointInfoService : IPlcPointInfoService
    {
        private readonly IMapper _mapper;
        private readonly IPlcPointInfoRepositpry _IPlcPointInfoRepositpry;
        private readonly ISnowflake _Snowflake;

        public PlcPointInfoService(IMapper mapper, IPlcPointInfoRepositpry plcPointInfoRepositpry, ISnowflake snowflake)
        {
            this._mapper = mapper;
            _IPlcPointInfoRepositpry = plcPointInfoRepositpry;
            _Snowflake = snowflake;
        }
        public Task<bool> DeletePlcPointInfoAsync(PlcPointInfoDto plcPointInfo)
        {
            throw new NotImplementedException();
        }

        public Task<bool> DeletePlcPointInfoByIdAsync(long id)
        {
            return _IPlcPointInfoRepositpry.DeleteByIdAsync(id);
        }

        /// <summary>
        /// 获取数据 分页获取
        /// </summary>
        /// <param name="queryPlcPointDto"></param>
        /// <returns></returns>
        public async Task<PageDto<PlcPointInfoDto>> GetPagedPlcPointInfoAsync(QueryPlcPointDto queryPlcPointDto)
        {

            PageModel page = new PageModel { PageIndex = queryPlcPointDto.PageIndex, PageSize = queryPlcPointDto.PageSize, };
            var plcConnectionID = queryPlcPointDto.PlcConnectID;
            var Floor = queryPlcPointDto.Floor;
            var ElevatorCode = queryPlcPointDto.ElevatorCode;
            var CrossroadCode = queryPlcPointDto.CrossroadCode;
            var Address = queryPlcPointDto.Address;

            var exp = Expressionable.Create<PlcPointInfo, PlcConnectionInfo>();
            exp.AndIF(plcConnectionID > 0, (a, b) => a.PlcConnectID == plcConnectionID);
            exp.AndIF(!string.IsNullOrEmpty(ElevatorCode), (a, b) => a.ElevatorCode.Contains(ElevatorCode));
            exp.AndIF(!string.IsNullOrEmpty(CrossroadCode), (a, b) => a.CrossroadCode.Contains(CrossroadCode));
            exp.AndIF(!string.IsNullOrEmpty(Address), (a, b) => a.Address.Contains(Address));
            exp.AndIF(Floor > 0, (a, b) => a.Floor == Floor);

            return await _IPlcPointInfoRepositpry.GetPlcPointInfoAsync(exp.ToExpression(), page);
        }

        public async Task<List<PlcPointInfo>> GetPlcPointInfoAsync(PlcPointInfo plcPointInfo)
        {
            var exp = SqlSugar.Expressionable.Create<PlcPointInfo>();
            var ID = plcPointInfo.ID;
            var Floor = plcPointInfo.Floor;
            var ElevatorCode = plcPointInfo.ElevatorCode;

            exp.AndIF(ID > 0, l => l.ID == ID);
            exp.AndIF(Floor > 0, l => l.Floor == Floor);
            exp.AndIF(!string.IsNullOrEmpty(ElevatorCode), l => l.ElevatorCode.Contains(ElevatorCode));
            return await _IPlcPointInfoRepositpry.GetListAsync(exp.ToExpression());
        }

        public async Task<PlcPointInfoDto> GetPlcPointInfoByIdAsync(long id)
        {
            var plcPointInfo = await _IPlcPointInfoRepositpry.GetByIdAsync(id);
            var plcPointInfoDto = _mapper.Map<PlcPointInfo, PlcPointInfoDto>(plcPointInfo);
            return plcPointInfoDto;
        }

        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="plcPointInfo"></param>
        /// <returns></returns>
        public async Task<bool> InsertPlcPointInfoAsync(PlcPointInfoDto plcPointInfoDto)
        {
            var plcPointInfo = _mapper.Map<PlcPointInfoDto, PlcPointInfo>(plcPointInfoDto);
            plcPointInfo.ID = _Snowflake.GetId();
            var res = await _IPlcPointInfoRepositpry.InsertAsync(plcPointInfo);
            return res;
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="plcPointInfo"></param>
        /// <returns></returns>
        public async Task<bool> UpdatePlcPointInfoAsync(PlcPointInfoDto plcPointInfoDto)
        {
            var plcPointInfo = _mapper.Map<PlcPointInfoDto, PlcPointInfo>(plcPointInfoDto);
            var isOK = await _IPlcPointInfoRepositpry.UpdateAsync(
                p => new PlcPointInfo
                {
                    Floor = plcPointInfo.Floor,
                    ElevatorCode = plcPointInfo.ElevatorCode,
                    PlcConnectID = plcPointInfo.PlcConnectID,
                    ItemName = plcPointInfo.ItemName,
                    ItemValue = plcPointInfo.ItemValue,
                    DataType = plcPointInfo.DataType,
                    CrossroadCode = plcPointInfo.CrossroadCode,
                    Address = plcPointInfo.Address,
                },
                p => p.ID == plcPointInfo.ID);

            return isOK;
        }
    }
}
